﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;

public class Common
    {
        public static string Domain = "http://huashu.qiyedaishu.com";
        public static string API = "http://huashu.qiyedaishu.com/tools/API.ashx";
        //public static string API = "http://localhost:2880/tools/API.ashx";
        public static string Upload = "http://huashu.qiyedaishu.com/tools/Upload.ashx";
        public static XmlDocument doc;
        public static XmlNodeList nodeList;
        static FileInfo xmlfile = new FileInfo(System.Windows.Forms.Application.StartupPath + @"/config.xml");
        static Common(){

        }
        public static void setXmlDoc(string rootStr)
        {
            doc = new XmlDocument();
            doc.Load(xmlfile.FullName);
            nodeList = doc.SelectSingleNode(rootStr).ChildNodes;
        }

        public static bool saveSystem(string username, string userpwd, string autologin, string rememberme)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "UserName")
                    {
                        xe.SetAttribute("value", username);
                    }
                    if (xe.Name == "UserPwd")
                    {
                        if (rememberme.ToLower() == "true")
                        {
                            xe.SetAttribute("value", userpwd);
                        }else
                        {
                            xe.SetAttribute("value", "");
                        }
                    }
                    if (xe.Name == "AutoLogin")
                    {
                        xe.SetAttribute("value", autologin);
                    }
                    if (xe.Name == "RememberMe")
                    {
                        xe.SetAttribute("value", rememberme);
                    }
            }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static string GetConfig(string name)
        {
            try
            {
                string val="";
                Common.setXmlDoc("root//System");
                foreach (XmlNode node in Common.nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == name)
                    {
                        val = xe.GetAttribute("value");
                        break;
                    }
                }
                return val;

            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="str"></param>
        public static void Log(string str)
        {
            string path = "D:/log.txt";
            if (!System.IO.File.Exists(path))
            {
                System.IO.FileStream f = System.IO.File.Create(path);
                f.Close();
                f.Dispose();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(path, true, System.Text.Encoding.GetEncoding("utf-8"));//true为追加,false为覆盖
            System.Text.Encoding.GetEncoding("gb2312");
            f2.WriteLine("=======================" + DateTime.Now + "=======================");
            f2.WriteLine(str);
            f2.Close();
            f2.Dispose();
        }

        public static int GetLength(string str)
        {
            byte[] sarr = System.Text.Encoding.Default.GetBytes(str);
            return sarr.Length;
        }

       

        public static long GetTimestamp()
        {
            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
            return (long)ts.TotalMilliseconds;
        }

        public static bool saveUser(string token)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "Token")
                    {
                        xe.SetAttribute("value", token);
                    }
                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool savePath(string token)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "Path")
                    {
                        xe.SetAttribute("value", token);
                    }
                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveSecond(string token)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "Second")
                    {
                        xe.SetAttribute("value", token);
                    }
                }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool savePublicColor(string publiccolor)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "PublicColor")
                    {
                        xe.SetAttribute("value", publiccolor);
                    }
            }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool savePrivateColor(string privatecolor)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "PrivateColor")
                    {
                        xe.SetAttribute("value", privatecolor);
                    }
            }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveKeywordColor(string keywordcolor)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "KeywordColor")
                    {
                        xe.SetAttribute("value", keywordcolor);
                    }
            }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveFixed(string fixeds)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "Fixed")
                    {
                        xe.SetAttribute("value", fixeds);
                    }
            }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static bool saveAutoSend(string autosend)
        {
            try
            {
                setXmlDoc("root//System");
                foreach (XmlNode node in nodeList)
                {
                    XmlElement xe = (XmlElement)node;
                    if (xe.Name == "AutoSend")
                    {
                        xe.SetAttribute("value", autosend);
                    }
            }
                doc.Save(xmlfile.FullName);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public static Image ImgToStream(string img)
        {
            WebRequest request = WebRequest.Create(img);
            WebResponse response = request.GetResponse();
            Stream s = response.GetResponseStream();
            Image image = Image.FromStream(s);
            s.Flush();
            s.Close();
            return image;
        }
        public static Image UrlToStream(string img)
        {
            WebRequest request = WebRequest.Create(img);
            WebResponse response = request.GetResponse();
            Stream s = response.GetResponseStream();
            Image image = Image.FromStream(s);
            s.Flush();
            s.Close();
            return image;
        }

        public static void DeleteFolder(string dir)
        {
            try
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                    {
                        FileInfo fi = new FileInfo(d);
                        if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                            fi.Attributes = FileAttributes.Normal;
                        File.Delete(d);//直接删除其中的文件 
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        public static Image Base64ToImg(string base64)
        {
            byte[] bytes = Convert.FromBase64String(base64);
            MemoryStream memStream = new MemoryStream(bytes);
            Image mImage = Image.FromStream(memStream);
            return mImage;
        }
}

